package com.goods.controller.business;

import com.goods.business.service.ProductService;
import com.goods.common.error.BaseError;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/*
 * post请求可以使用map集合去接收前端的数据。
 *
 * */

/**
 * @author XiaoHong
 * @description 物资资料的接口
 * @date 2022/2/17
 */
@RestController
@Api(tags = "物资资料的接口")
@RequestMapping("/business/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    //物资资料分页列表展示:用户可以根据分类、物资名称（模糊）和物资状态进行条件查询
    //business/product/findProductList
    @GetMapping("/findProductList")
        public ResponseBean<PageVO<ProductVO>> findProductList(
                @RequestParam("pageNum") Integer pageNum,
                @RequestParam("pageSize") Integer pageSize,
                @RequestParam Map map   //实际上传过来的几个过滤条件用实体类接受了；
    ) {
        System.out.println(map);

        PageVO<ProductVO> productVOPageVO = productService.findProductList(pageNum, pageSize, map);


        return ResponseBean.success(productVOPageVO);

    }


    //添加物资
    //business/product/add
    @PostMapping("/add")
//    public ResponseBean add(@RequestBody Map<String,Object> map){  第一种方式
    public ResponseBean add(@RequestBody Product product) throws BusinessException {   //第二种方式
        System.out.println(product);
        if (product.getCategoryKeys().size() != 3) {
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR);
        }
        productService.add(product);
        return ResponseBean.success();
    }

    //http://www.localhost:8989/business/product/edit/58
    //进入编辑物资界面
    @GetMapping("/edit/{productId}")
    public ResponseBean<Product> edit(@PathVariable Long productId) {
        Product product = productService.edit(productId);
        return ResponseBean.success(product);//
    }

    //物资编辑界面点击修改确认
    @PutMapping("/update/{productId}")
    public ResponseBean update(@RequestBody Product Product,
                               @PathVariable Long productId) {

        productService.update(Product, productId);

        return ResponseBean.success();
    }


    // http://www.localhost:8989/business/product/remove/57
    //将物资移入到回收站
    @PutMapping("/remove/{productId}")
    public ResponseBean remove(@PathVariable Long productId) {
        productService.remove(productId);
        return ResponseBean.success();
    }

    //http://www.localhost:8989/business/product/back/51
    //将物资从回收站恢复
    @PutMapping("/back/{productId}")
    public ResponseBean back(@PathVariable Long productId) {
        productService.back(productId);
        return ResponseBean.success();
    }

    //http://www.localhost:8989/business/product/delete/37
    //将物资删除
    @DeleteMapping("/delete/{productId}")
    public ResponseBean delete(@PathVariable Long productId) {
        productService.delete(productId);
        return ResponseBean.success();
    }


    //http://www.localhost:8989/business/product/publish/54
//将物资通过审核
    @PutMapping("/publish/{productId}")
    public ResponseBean publish(@PathVariable Long productId) {
        productService.publish(productId);
        return ResponseBean.success();
    }

    //加载商品列表(可入库)
   //www.localhost:8989/business/product/findProducts?pageNum=1&pageSize=6&status=0
   @GetMapping("/findProducts")
   public ResponseBean<PageVO<ProductVO>> findProducts(
           @RequestParam("pageNum") Integer pageNum,
           @RequestParam("pageSize") Integer pageSize,
           @RequestParam Map map   //实际上传过来的几个过滤条件用实体类接受了；
   ) {
       System.out.println(map);

       PageVO<ProductVO> productVOPageVO = productService.findProductList(pageNum, pageSize, map);


       return ResponseBean.success(productVOPageVO);

   }


    // http://www.localhost:8989/business/product/findProductStocks?pageSize=9&pageNum=1&categorys=33,34,37
    //加载分类下的库存信息
    @GetMapping("/findProductStocks")
    public ResponseBean<PageVO<ProductStockVO>> findProductStocks(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "categorys",required = false) String categorys,
            @RequestParam(value = "name" ,required = false) String name){


        PageVO<ProductStockVO> productStockVOPage = productService.findProductStocks(pageNum, pageSize, categorys,name);


        return ResponseBean.success(productStockVOPage);

    }

//    物资所有的库存信息
    //http://www.localhost:8989/business/product/findAllStocks?pageSize=9&pageNum=1&name=
@GetMapping("/findAllStocks")
public ResponseBean<List<Map>> findAllStocks(
        @RequestParam("pageNum") Integer pageNum,
        @RequestParam("pageSize") Integer pageSize,
        @RequestParam(value = "categorys",required = false) String categorys,
        @RequestParam(value = "name" ,required = false) String name){


    List<Map> mapList = productService.findAllStocks(pageNum, pageSize, categorys,name);


    return ResponseBean.success(mapList);

}
}
